package xyz.lne.researchgroupmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.lne.researchgroupmanage.controller.response.KeywordResponse;
import xyz.lne.researchgroupmanage.entity.*;
import xyz.lne.researchgroupmanage.exception.BadRequestException;
import xyz.lne.researchgroupmanage.mapper.KeywordMapper;
import xyz.lne.researchgroupmanage.mapper.KeywordResearchMapper;
import xyz.lne.researchgroupmanage.mapper.ResearchMapper;
import xyz.lne.researchgroupmanage.mapper.SysUserMapper;
import xyz.lne.researchgroupmanage.service.KeywordResearchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.lne.researchgroupmanage.service.dataTransferObject.ResearchSimpleDTO;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@Service
public class KeywordResearchServiceImpl extends ServiceImpl<KeywordResearchMapper, KeywordResearch> implements KeywordResearchService {

    @Autowired
    private KeywordResearchMapper keywordResearchMapper;

    @Autowired
    private KeywordMapper keywordMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ResearchMapper researchMapper;


    @Override
    public List<ResearchSimpleDTO> findResearchSimpleByKeywords(List<String> keywordList) {
        List<ResearchSimpleDTO> researchSimpleDTOS = new LinkedList<>();

        keywordList.forEach(word -> {
            QueryWrapper<Keyword> keywordQueryWrapper = new QueryWrapper<>();
            keywordQueryWrapper.eq("word", word);
            Keyword keyword = keywordMapper.selectOne(keywordQueryWrapper);

            QueryWrapper<KeywordResearch> keywordResearchQueryWrapper = new QueryWrapper<>();
            keywordResearchQueryWrapper.eq("keyword_id", keyword.getKeywordId());
            List<KeywordResearch> keywordResearches = keywordResearchMapper.selectList(keywordResearchQueryWrapper);

            keywordResearches.forEach(keywordResearch -> {
                ResearchSimpleDTO researchSimpleDTO = new ResearchSimpleDTO();
                Research research = researchMapper.selectById(keywordResearch.getResearchId());
                BeanUtils.copyProperties(research, researchSimpleDTO);
                researchSimpleDTO.setUserName(sysUserMapper.selectById(research.getUserId()).getName());
                researchSimpleDTOS.add(researchSimpleDTO);

            });
        });
        return researchSimpleDTOS;
    }

    @Override
    public Boolean deleteKeywordByResearchId(Integer researchId) {
        QueryWrapper<KeywordResearch> wrapper=new QueryWrapper<>();
        wrapper.eq("research_id",researchId);
        keywordResearchMapper.delete(wrapper);
        return true;
    }

    @Override
    public Boolean insertKeyword(Integer keywordId, Integer researchId) {
        QueryWrapper<Keyword> KWrapper=new QueryWrapper<>();
        KWrapper.eq("keyword_id", keywordId);

        QueryWrapper<Research> RGWrapper=new QueryWrapper<>();
        RGWrapper.eq("research_id",researchId);

        QueryWrapper<KeywordResearch> KRWrapper=new QueryWrapper<>();
        KRWrapper.eq("keyword_id", keywordId)
                .eq("research_id",researchId);

        if(keywordMapper.selectCount(KWrapper)==0 || researchMapper.selectCount(RGWrapper)==0){
            //TODO:根据需求确定抛出异常或返回false
            Map<String, Object> e = new HashMap<>();
            e.put("课题组不存在或关键词不存在", null);
            throw new BadRequestException(e);
        }

        if(keywordResearchMapper.selectCount(KRWrapper)>0){
            //TODO:已有该条记录时确定适当的返回值
            return true;
        }
        keywordResearchMapper.insert(new KeywordResearch(keywordId,researchId));

        return true;
    }
}
